import {
  OrderModel
} from "../../../models/order";
import {
  Format
}
from "../../../utils/common";
const orderModel = new OrderModel()
const format = new Format()
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
        title: '加油记录',
      },
      {
        title: '充值记录',
      },
      {
        title: '积分记录'
      }
    ],
    activeTab: 0,
    // list: '默认值',
    page: 0,
    show: false, 
    openTipPop: false, // 是否显示弹窗
    TipTxt: '', // 弹窗内容
    tips: '', // 点击按钮方法
    loadingText: '加载中......', //加载中文字
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.activeTab)
    if (options.activeTab) {
      this.setData({
        activeTab: parseInt(options.activeTab)
      })
    }
    console.log(this.data.activeTab)
    if (options.endTime) {
      this.setData({
        startTime: options.startTime,
        endTime: options.endTime,
      })
    }
    this.loadOrders(0)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    console.log(options)
    console.log(my.getStorageSync({ key: 'activeTab' }))
    let activeTab = my.getStorageSync({ key: 'activeTab' })
    let time = my.getStorageSync({ key: 'time' })
    console.log(my.getStorageSync({ key: 'time' }))
    if ((activeTab.data || activeTab.data == 0) && time.data) {
      console.log(activeTab + time)
      this.setData({
        activeTab: parseInt(activeTab.data),
        startTime: time.data.startTime,
        endTime: time.data.endTime,
      })
      this.loadOrders(0)
    }
  },
  loadOrders(begin) {
    let vm = this
    var startTime= this.data.startTime
    var endTime = this.data.endTime
    if (!startTime && !endTime) {
      var myDate = new Date();
      var tYear = myDate.getFullYear();
      var tMonth = myDate.getMonth();
      startTime = format.date("yyyy-MM", new Date()) + '-01'
      endTime = format.date("yyyy-MM", new Date()) + '-31'
    } else {
      // vm.setData({
      //   list: ''
      // })
    }
    orderModel.loadOrders(startTime, endTime,this.data.activeTab+1,begin,6,app.globalData.user.phone).then(res => {
      if (res) {
        console.log(vm.data.page)
        if (vm.data.page == 0) {
          vm.setData({
            list: res
          })
        } else {
          if ( res.length > 0) {
            let list = this.data.list;
            for (let i = 0; i < res.length; i++) {
              list.push(res[i]);
            }
            vm.setData({
              list,
              show: false
            });
          } else {
            vm.setData({
              show: false,
              page: this.data.page - 1,
              loadingText: '已经到底了'
            });
          }
          
        }
      }
    })
  },
  changeList() {
    const activeTab = this.data.activeTab
    let list = []
    const listAll = this.data.listAll
    if (activeTab == 0) {
      list = listAll.unuseList
    } else if (activeTab == 1) {
      list = listAll.usedList
    } else if (activeTab == 2) {
      list = listAll.overdueList
    }
    this.setData({
      list
    })
  },
  scrollMytrip2() {
    try{
      this.setData({ 
        show: true,
        page: this.data.page + 1
      });
      this.loadOrders(this.data.page)
    } catch(e) {
      this.setData({ show: false });
      console.log('scrollMytrip执行异常:', e);
    }
  },
  toDetail(e){
    let tradeNo = ''
    if (e.currentTarget.dataset.tradeNo) {
      tradeNo = e.currentTarget.dataset.tradeNo
    } else {
      tradeNo = e.currentTarget.dataset.orderId
    }
    let orderId = e.currentTarget.dataset.orderId
    let amount = e.currentTarget.dataset.createAmount
    console.log(tradeNo)
    my.navigateTo({
      url: '/pages/orderDetail/orderDetail?tradeNo=' + tradeNo + '&orderId=' +orderId + '&amount=' + amount
    })
    
  },
  tabChange(index) {
    this.setData({
      activeTab: index,
      page: 0,
      list: '',
      startTime: '',
      endTime: '',
    });
    my.removeStorageSync({
      key: 'time',
    });
    this.loadOrders(0)
  },
  handleTabClick({ index}) {
    this.setData({
      activeTab: index,
      page: 0,
      list: '',
      startTime: '',
      endTime: '',
    });
    my.removeStorageSync({
      key: 'time',
    });
    this.loadOrders(0)
  },
  handleTabChange({index}) {
    this.setData({
      activeTab: index,
      page: 0,
      list: '',
      startTime: '',
      endTime: '',
    });
    my.removeStorageSync({
      key: 'time',
    });
    this.loadOrders(0)
  },
  goCalendar() {
    console.log(this.data)
    my.removeStorageSync({
      key: 'time',
    });
    my.setStorageSync({
      key: 'activeTab',
      data: this.data.activeTab
    });
    my.navigateTo({
      url: '/pages/calendar/calendar'
    })
    // my.redirectTo({
    //   url: '/pages/calendar/calendar?activeTab=' + this.data.activeTab
    // })
  },
  // 点击知道了
  tipPopSure(tips) {
    this.setData({
      openTipPop: false
    });
    if (tips == 'href') {
      my.navigateBack('1')
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})